<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          size="mini"
          clearable
          placeholder="学生姓名"
          @keyup.enter.native="handleQuery"
        >
        </el-input>
      </el-form-item>

      <el-form-item label="时间">
        <el-date-picker
          v-model="queryParams.dateList"
          size="small"
          style="width: 380px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          :default-time="['00:00:00', '23:59:59']"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeTime"
        />
      </el-form-item>
      <el-form-item label="进出类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          size="warning"
          type="mini"
          icon="el-icon-download"
          @click="exportTrajectory"
        >
          导出
        </el-button>
        <el-button
          size="danger"
          type="mini"
          icon="el-icon-refresh"
          @click="refreshData"
        >
          {{refreshText}}
        </el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button size="warning" type="mini" icon="el-icon-download"  @click="exportTrajectory">
          导出
        </el-button>
      </el-col>
    </el-row> -->
    <el-row>
      <el-col :span="16">
        <el-table v-loading="loading" :data="list" highlight-current-row
    @current-change="handleCurrentChange" style="width: 100%">
      <!-- <el-table-column label="照片" align="center" prop="snap_ftp_url">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.snapFtpUrl"
            style="width: 100px; height: 100px"
            :preview-src-list="[scope.row.snapFtpUrl]"
          >
          </el-image>
        </template>
      </el-table-column> -->
      <el-table-column
        label="学生ID"
        align="center"
        prop="studentId"
        :show-overflow-tooltip="true"
        width="100"
      />
      <el-table-column
        label="学生姓名"
        align="center"
        prop="studentName"
        :show-overflow-tooltip="true"
        width="100"
      />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="刷脸设备" align="center" prop="deviceAddr" />
      <el-table-column label="刷脸时间" align="center" prop="faceTime" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <label v-if="scope.row.type == 1" style="color: #52c41a"
            >进入校园</label
          >
          <label v-if="scope.row.type == 2" style="color: #faad14"
            >离开校园</label
          >
          <label v-if="scope.row.enterType == 3">宿舍打卡</label>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      </el-col>
      <el-col :span="1">
        &nbsp;
      </el-col>
      <el-col :span="7">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{bigImage.studentName}}({{ bigImage.className }})</span>
            <span style="float: right; padding: 3px 0">
              <label v-if="bigImage.type == 1" style="color: #52c41a">进入校园</label>
              <label v-if="bigImage.type == 2" style="color: #faad14">离开校园</label>
            </span>
            
          </div>
          <el-image :src="bigImage.snapFtpUrl" :preview-src-list="[bigImage.snapFtpUrl]" ></el-image>
        </el-card>
      </el-col>
    </el-row>
    
  </div>
</template>
  
  <script>
import { findPageList, exportTrajectory } from "@/api/studentInfo/trajectory";

export default {
  name: "trajectory",
  data() {
    return {
      refreshText:"停止更新",
      refreshStatus:true,
      bigImage:{},
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      queryParams: {
        pageNum: 1, //页码
        pageSize: 10, //每页大小
        dateList: [],
      },
      options: [
        {
          value: "1",
          label: "进入校园",
        },
        {
          value: "2",
          label: "离开校园",
        },
      ],
    };
  },
  created() {
    var curDate = new Date();
    this.queryParams.dateList = [
      this.parseTime(new Date(curDate.getTime() - 86400 * 1000)),
      this.parseTime(new Date()),
    ];
    // this.getList();
    this.setTimer()
  },
  beforeDestroy(){
    this.refreshStatus = false
    console.log("离开页面")
  },
  methods: {
    refreshData(){
      if(this.refreshStatus){
        this.refreshStatus = false
        this.refreshText = "开始更新"
      }else{
        this.refreshStatus = true
        this.refreshText = "停止更新"
        this.setTimer()
      }
    },
    setTimer () {
      if(this.refreshStatus==false){
        return
      }
      let timer
      this.resetQuery()
      
      timer = setTimeout(() => {
            this.setTimer()
           
        }, 3000)  
      }, 
    handleCurrentChange(row){
      this.bigImage = row
    },
    changeTime(val) {
      console.log(val);
    },
    resetQuery() {
      var curDate = new Date();
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        dateList: [
          this.parseTime(new Date(curDate.getTime() - 186400 * 1000)),
          this.parseTime(new Date()),
        ],
      };
      this.getList();
    },

    /** 查询门禁流水列表 */
    getList() {
      // this.loading = true;
      if (this.queryParams.dateList.length > 0) {
        this.queryParams.beginTime = this.queryParams.dateList[0];
        this.queryParams.endTime = this.queryParams.dateList[1];
      } else {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
      findPageList(this.queryParams).then((res) => {
        // console.log(res);
        this.list = res.data.list;
        if(this.list.length>0){
          this.bigImage = this.list[0]
        }
        this.total = res.data.total;
        // this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },

    exportTrajectory() {
      if (this.queryParams.dateList.length > 0) {
        this.queryParams.beginTime = this.queryParams.dateList[0];
        this.queryParams.endTime = this.queryParams.dateList[1];
      } else {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
      exportTrajectory(this.queryParams).then((resp) => {
        this.fileDownload(resp.data, "学生出入记录.xlsx");
      });
    },
  },
};
</script>
  
  